package com.example.demo.utils;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.sql.parser.SQLStatementParser;

import java.sql.SQLSyntaxErrorException;
import java.util.List;

public class ParserMain {

    public static void main(String[] args) throws SQLSyntaxErrorException {
        String sql = "select user.id,user.name from user user,user1 user1";
        String sql2 = "select user.id,user.name from user user left join user1 user1 on user.id = user1.id where 1 = 1 and user.id = 3";

        // 新建 MySQL Parser
        SQLStatementParser parser = new MySqlStatementParser(sql2);

        // 使用Parser解析生成AST，这里SQLStatement就是AST
        SQLStatement statement = parser.parseStatement();

        // 使用visitor来访问AST
        MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
        statement.accept(visitor);
        // 从visitor中拿出你所关注的信息
        System.out.println(visitor.getColumns());
        System.out.println(visitor.getTables());
        System.out.println(visitor.getConditions());
        ParserMain pm = new ParserMain();
        String out = pm.execMySql("select * from user where 1 = 1");
        System.out.println(out);
        test1();

    }

    private String execMySql(String sql2) {
        String sql = "select id, name from table where id = 100 and name='jluo';";
        SQLStatementParser parser = new MySqlStatementParser(sql);
        List<SQLStatement> stmtList = parser.parseStatementList();
        MySqlExportParameterVisitor pVisitor = new MySqlExportParameterVisitor();
        for (SQLStatement stmt : stmtList) {
            stmt.accept(pVisitor);
            List<Object> list = pVisitor.getParameters();
            for (Object obj : list) {
                System.out.println(obj);
            }
        }
        return null;
    }

    public static SQLStatement parser(String sql,String dbType) throws SQLSyntaxErrorException {
        List<SQLStatement> list = SQLUtils.parseStatements(sql, dbType);
        if (list.size() > 1) {
            throw new SQLSyntaxErrorException("MultiQueries is not supported,use single query instead ");
        }
        return list.get(0);
    }
    public static void test1() throws SQLSyntaxErrorException {
        String sql = "select  name ,id from (select * from user) t where t.id =10";
//        String sql = "select  name ,id from t where t.id =10";
        String dbType = "mysql";
        System.out.println("原始SQL 为 ： "+sql);
        SQLSelectStatement statement = (SQLSelectStatement) parser(sql, dbType);
        SQLSelect select = statement.getSelect();
        SQLSelectQueryBlock query = (SQLSelectQueryBlock) select.getQuery();
        SQLExprTableSource tableSource = (SQLExprTableSource) query.getFrom();
        String tableName = tableSource.getExpr().toString();
        System.out.println("获取的表名为  tableName ：" + tableName);
        //修改表名为acct_1
        tableSource.setExpr("acct_1");
        System.out.println("修改表名后的SQL 为 ： [" + statement.toString() +"]");
    }
}